#============================================================
#    Scirpt Util
#============================================================
# - datetime: 2022-07-17 17:25:00
#============================================================

class_name ScriptUtil


##  获取属性列表
static func property_list(script: Script) -> Array[Dictionary]:
	return script.get_script_property_list()

##  获取方法列表
static func method_list(script: Script) -> Array[Dictionary]:
	return script.get_script_method_list()

##  获取信号列表
static func signal_list(script: Script) -> Array[Dictionary]:
	return script.get_script_signal_list()

##  获取方法数据
## [br]
## [br][code]script[/code]： 脚本
## [br][code]method[/code]： 要获取的方法数据的方法名
## [br]
## [br][code]return[/code]： 返回脚本的数据信息。
##  包括的 key 有 [code]name[/code], [code]args[/code], [code]default_args[/code]
## , [code]flags[/code], [code]return[/code], [code]id[/code]
func find_method_data(script: Script, method: String) -> Dictionary:
	var method_data = method_list(script)
	for m in method_data:
		if m['name'] == method:
			return m
	return {}

##  获取扩展脚本链（扩展的所有脚本）
## @object  Object 对象或脚本
## @return  返回继承的脚本路径列表
static func get_extends_link(script: Script) -> Array:
	var list := []
	while script:
		list.push_back(script.resource_path)
		script = script.get_base_script()
	return list

## 数据类型名称
##[br][code]type[/code]: 数据类型枚举值
##[br][code]return[/code]: 返回数据类型的字符串
static func data_type_name(type: int) -> String:
	type = int(type)
	match type:
		TYPE_NIL:
			return ""
		TYPE_BOOL:
			return "bool"
		TYPE_INT:
			return "int"
		TYPE_FLOAT:
			return "float"
		TYPE_STRING:
			return "String"
		TYPE_VECTOR2:
			return "Vector2"
		TYPE_RECT2:
			return "Rect2"
		TYPE_VECTOR3:
			return "Vector3"
		TYPE_TRANSFORM2D:
			return "Transform2D"
		TYPE_PLANE:
			return "Plane"
		TYPE_QUATERNION:
			return "Quat"
		TYPE_AABB:
			return "AABB"
		TYPE_BASIS:
			return "Basis"
		TYPE_TRANSFORM2D:
			return "Transform2D"
		TYPE_TRANSFORM3D:
			return "Transform3D"
		TYPE_COLOR:
			return "Color"
		TYPE_NODE_PATH:
			return "NodePath"
		TYPE_RID:
			return "RID"
		TYPE_DICTIONARY:
			return "Dictionary"
		TYPE_ARRAY \
		, TYPE_PACKED_BYTE_ARRAY \
		, TYPE_PACKED_INT32_ARRAY \
		, TYPE_PACKED_INT64_ARRAY \
		, TYPE_PACKED_STRING_ARRAY \
		, TYPE_PACKED_VECTOR2_ARRAY \
		, TYPE_PACKED_VECTOR3_ARRAY \
		, TYPE_PACKED_FLOAT32_ARRAY \
		, TYPE_PACKED_FLOAT64_ARRAY \
		, TYPE_PACKED_COLOR_ARRAY \
		:
			return "Array"
#		TYPE_OBJECT:
#			return "Object"
		_:
			return ""


##  方法数据转为代码
static func method_to_func_code(method_data: Dictionary) -> String:
	var temp := method_data.duplicate(true)
	var args := ""
	for i in temp['args']:
		var arg_name = i['name']
		var arg_type = ( data_type_name(i['type']) if i['type'] != TYPE_NIL else "")
		if arg_type.strip_edges() == "":
			arg_type = str(i['class_name'])
		if arg_type.strip_edges() != "":
			arg_type = ": " + arg_type
		args += "%s%s, " % [arg_name, arg_type]
	temp['args'] = args.trim_suffix(", ")
	if temp['return']['type'] != TYPE_NIL:
		temp['return_type'] = data_type_name(temp['return']['type'])
	
	if temp.has('return_type') and temp['return_type'] != "":
		temp['return_type'] = " -> " + str(temp['return_type'])
		temp['return_sentence'] = "pass\n\treturn super." + temp['name'] + "()"
	else:
		temp['return_type'] = ""
		temp['return_sentence'] = "pass"
	
	return "func {name}({args}){return_type}:\n\t{return_sentence}\n".format(temp)


##  对象是否是 tool 状态
##[br]
##[br][code]object[/code]  返回这个对象的脚本是否是开启 tool 的状态
static func is_tool(object: Object) -> bool:
	var script 
	if script is Script:
		script = object
	else:
		script = object.get_script()
	if script is Script:
		return script.is_tool()
	return false

